﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.DTO.ProductionSystem;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    public class ProductionWasteOrderController : Controller
    {
        private readonly IProductionWasteOrderServices productionWasteOrderServices;

        public ProductionWasteOrderController(IProductionWasteOrderServices productionWasteOrderServices)
        {
            this.productionWasteOrderServices = productionWasteOrderServices;
        }

        public IActionResult ProductionWasteOrderView()
        {
            return View();
        }
        /// <summary>
        /// 新增生产废料
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddProductionWasteOrder([FromBody]ProductionWasteOrderDTO dto)
        {
            return await productionWasteOrderServices.AddProductionWasteOrder(dto);
        }

        /// <summary>
        /// 删除生产废料
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DelProductionWasteOrder([FromBody]List<int> ids)
        {
            await productionWasteOrderServices.DelProductionWasteOrder(ids);
        }

        /// <summary>
        /// 获取生产废料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ProductionWasteOrderDTO> GetProductionWasteOrder(int id)
        {
            return await productionWasteOrderServices.GetProductionWasteOrder(id);
        }

        /// <summary>
        /// 生产废料列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="wasteOrderName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowProductionWasteOrder(int pageIndex = 1, int pageSize = 10, string? wasteOrderName = "")
        {
            var list=await productionWasteOrderServices.ShowProductionWasteOrder(pageIndex, pageSize, wasteOrderName);
            var result = list.OrderByDescending(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code=0,
                msg="",
                count=list.Count,
                data=result
            });
        }

        /// <summary>
        /// 编辑生产废料
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateProductionWasteOrder([FromBody]ProductionWasteOrderDTO dto)
        {
            return await productionWasteOrderServices.UpdateProductionWasteOrder(dto);
        }

        public async Task<IActionResult> EditProductionWasteOrderView(int? id)
        {
            ProductionWasteOrderDTO model = new ProductionWasteOrderDTO();
            if (id.HasValue && id.Value > 0)
            {
                model = await productionWasteOrderServices.GetProductionWasteOrder(id.Value);
            }
            return View(model);
        }
    }
}
